package org.qiuqiu.tools;

import java.util.HashMap;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @author 杜秋野
 *
 */
public class NameTools {

	// 返回姓氏的字母索引，比如刘德华，返回l
	public static String getNameIndex(String str) {
		return getNameHeadChar(str).substring(0, 1);
	}

	// 返回姓名全称的首字母，比如刘德华，返回ldh
	public static String getNameHeadChar(String str) {

		String convert = "";
		for (int j = 0; j < str.length(); j++) {
			char word = str.charAt(j);
			String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
			if (pinyinArray != null) {
				convert += pinyinArray[0].charAt(0);
			} else {
				convert += word;
			}
		}
		return convert;
	}

	// 返回姓氏的拼音，比如刘德华，返回liu
	public static String getXing(String str) {
		return getPinYin(str.substring(0, 1));
	}

	// 返回姓名全称的汉语拼音，比如刘德华，返回liudehua
	public static String getPinYin(String src) {
		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符
				if (java.lang.Character.toString(t1[i]).matches(
						"[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				} else
					t4 += java.lang.Character.toString(t1[i]);
			}
			// System.out.println(t4);
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return t4;
	}

	// 将字符串转移为ASCII码
	public static String getCnASCII(String cnStr) {
		StringBuffer strBuf = new StringBuffer();
		byte[] bGBK = cnStr.getBytes();
		for (int i = 0; i < bGBK.length; i++) {
			// System.out.println(Integer.toHexString(bGBK[i]&0xff));
			strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
		}
		return strBuf.toString();
	}

	// 将input中的汉字和拼音进行拆分并放入中
	public static HashMap<String, String> splitInput(String input) {
		input = input.replace(" ", "");
		HashMap<String, String> hm = new HashMap<String, String>();
		String regExChinese = "[\\u4e00-\\u9fa5]+";
		String regExEnglish = "[a-zA-Z]+";
		String name = "";
		String pinyin = "";

		name = input.replaceAll(regExEnglish, "%");
		pinyin = input.replaceAll(regExChinese, "%");

		name = addPercent(name);
		pinyin = addPercent(pinyin);
		hm.put("name", name);
		hm.put("pinyin", pinyin);
		return hm;
	}

	// 去掉前后的"%"
	public static String delPercent(String str) {
		if (str.startsWith("%")) {
			str = str.substring(1);
		}
		if (str.endsWith("%")) {
			str = str.substring(0, str.length() - 1);
		}
		return str;
	}
	
	//把前后都加上%
	public static String addPercent(String str){
		if(!str.startsWith("%")){
			str = "%"+str;
		}
		if(!str.endsWith("%")){
			str = str+"%";
		}
		return str;
	}

	public static void main(String args[]) {
		HashMap<String, String> hm = new HashMap<String, String>();
		String input = "张彬";
		hm = splitInput(input);
		System.out.println("name:" + hm.get("name"));
		System.out.println("pinyin:" + hm.get("pinyin"));
	}
}